<!-- 商品详情底部栏组件 -->
<template>
  <div class="detail-bottom">
    <div class="img" @click="$emit('toast')">
      <img src="@/images/basket.png" alt="">
      <div class="mark" v-if="hasProduct">{{ count }}</div>
    </div>
    <div class="price">
      <div class="total" v-if="hasProduct">总计:<span>￥{{ total }}</span></div>
      <div v-if="!hasProduct">购物车是空的</div>
    </div>
    <div class="topay" @click="handleClick">去结算</div>
  </div>
</template>

<script>
import { ref, toRefs, watchEffect } from 'vue';
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
// 显示相关
export const useDispaly = (state, id) => {
  const total = ref(0);
  const count = ref(0);
  const hasProduct = ref(false);
  watchEffect(() => {
    const productList = state.cartList[id];
    total.value = count.value = 0;
    hasProduct.value = false;
    if (productList && Object.keys(productList).length > 0) {
      hasProduct.value = true;
      for (const item in productList) {
        if (productList[item].check) {
          total.value += productList[item].price * productList[item].count;
          count.value += productList[item].count;
        }
      }
      total.value = Math.round(total.value * 100) / 100
    }
  })
  return { total, hasProduct, count }
}
// 支付相关
const usePay = (total, router, id) => {
  const handleClick = () => {
    if (total.value === 0) alert('没有订单');
    else router.push({ name: 'confirm', query: { id, total: total.value } })
  }
  return { handleClick }
}
// composition
export default {
  name: 'detailBottom',
  props: ['shopId'],
  setup(props) {
    const store = useStore()
    const router = useRouter()
    const { shopId } = toRefs(props)
    const { hasProduct, total, count } = useDispaly(store.state, shopId.value)
    const { handleClick } = usePay(total, router, shopId.value)
    return { hasProduct, total, count, handleClick }
  }
}
</script>

<style scoped lang="scss">
@import '@/styles/mixin.scss';
@import '@/styles/theme.scss';

.detail-bottom {
  position: fixed;
  width: 100%;
  height: 49px;
  bottom: 0;
  left: 0;
  box-shadow: 0 -1px 1px 0 #F1F1F1;
  display: flex;

  .img {
    width: 20.2667vw;
    @include item-center;
    position: relative;

    img {
      width: 28px;
      height: 26px;
    }

    .mark {
      position: absolute;
      top: 0;
      left: 0;
      margin-left: 12vw;
      border-radius: 10px;
      line-height: 20px;
      background-color: $remind-color;
      font-size: 16px;
      color: $white;
      padding: 0 5px;
      transform-origin: left center;
      transform: scale(.5);
    }
  }

  .price {
    flex: auto;
    padding-left: 8px;
    line-height: 49px;
    font-size: 12px;
    color: $theme-font-color;

    .total {
      display: flex;
      align-items: center;

      span {
        color: $remind-color;
        font-size: 18px;
      }
    }
  }

  .topay {
    width: 26.1333vw;
    color: $white;
    font-size: 14px;
    background-color: #4FB0F9;
    line-height: 49px;
    text-align: center;
  }
}
</style>
